import random

from core.models import *
from django.contrib.auth.models import User
from django.utils import timezone
from django.db.models import Avg, Value, CharField, Sum, Count, F, Q, Case, When, Min, Max, OuterRef, Subquery, Exists
from django.db.models.functions import Lower, Concat, Coalesce
from django.db import transaction
import itertools
import time
from django.contrib.contenttypes.models import ContentType


def run():
    comments = Comment.objects.all()
    for comment in comments:
        print(type(comment.content_object))  # a model class object  <class 'core.models.Rating'>

    print('================')
    comment: Comment = Comment.objects.first()

    # get the stored content type
    ctype: ContentType = comment.content_type  # <class 'django.contrib.contenttypes.models.ContentType'>
    print(ctype)

    # from the content type, fetch the associated generic FK instance
    model = ctype.get_object_for_this_type(pk=comment.object_id)
    print(model)

    # restaurant: Restaurant = Restaurant.objects.first()
    # comment = Comment.objects.create(
    #     text='Awful restaurant',
    #     content_object=restaurant
    # )
    # print(comment)
    restaurant = Restaurant.objects.get(pk=3)
    # restaurant.comments.add(
    #     Comment.objects.create(text='text 1', content_object=restaurant)
    # )
    comments = restaurant.comments.all()
    print(comments)

    print('-----------------------------------------')
    # comments = Comment.objects.filter(
    #     restaurant__restaurant_type=Restaurant.TypeChoices.CHINESE
    # )
    # print(comments)
    restaurant = Restaurant.objects.prefetch_related('comments').filter(pk=3).first()
    print(restaurant.comments.all())


